package com.only4play.usercenter.process;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author liyuncong
 * @version 1.0
 * @file LogThreadFactory
 * @brief 日志线程工厂
 * @details 日志线程工厂
 * @date 2024-05-21
 *
 * Edit History
 * ----------------------------------------------------------------------------
 * DATE                     NAME               DESCRIPTION
 * 2024-05-21               liyuncong          Created
 */

@Slf4j
public class LogThreadFactory implements ThreadFactory {

    public LogThreadFactory(String namePrefix) {
        this.namePrefix = namePrefix;
    }

    private final String namePrefix;

    private final AtomicInteger nextId = new AtomicInteger(1);

    @Override
    public Thread newThread(Runnable runnable) {
        String name = namePrefix + nextId.getAndIncrement();
        Thread thread = new Thread(null, runnable, name, 0);
        log.info("LogThreadFactory threadName:{}", thread.getName());
        return thread;
    }
}
